package cn.ly.design.action.state;

public class MementoTest {

	public static void main(String[] args) {
		
		Original o = new Original("test1");
		Storage s = new Storage(o.createMemento());
		
		System.out.println("初始化状态为：" + o.getValue());  
        o.setValue("test2");  
        System.out.println("修改后的状态为：" + o.getValue());  
        
        o.restoreMemento(s.getMemento());
        System.out.println("恢复后的状态为：" + o.getValue());  
        
	}
	
}

class Original{
	private String value;
	public Original(String value){
		this.value = value;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public Memento createMemento(){
		return new Memento(value);
	}
	public void restoreMemento(Memento m){
		this.value = m.getValue();
	}
}

class Memento{
	private String value;
	public Memento(String value){
		this.value = value;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
}

class Storage {  
    
    private Memento memento;  
      
    public Storage(Memento memento) {  
        this.memento = memento;  
    }  
  
    public Memento getMemento() {  
        return memento;  
    }  
  
    public void setMemento(Memento memento) {  
        this.memento = memento;  
    }  
}  

